#ifndef _MOTOR_DRV_H__
#define _MOTOR_DRV_H__

#include "can_config.h"

#define ENCODER_PLUSE_ROUND         131072L       //编码器一圈的脉冲数量

using namespace std;

typedef enum
{
    CIA_NOT_READY_SWITCH_ON = 0,
    CIA_SWITCH_ON_DISABLE,
    CIA_READY_SWITCH_ON,
    CIA_SWITCH_ON,
    CIA_OPERATION_ENABLE,
    CIA_QUCIK_STOP,
    CIA_FAULT,
    CIA_FAULT_REACTION,
}CIA402_STATE_e;

typedef struct 
{
    float ratedTorque;          //额定力矩, Nm
    float maxSpeed;             //最大转速, RPM
    int encoderResolution;      //编码器分辨率, 单圈脉冲数量
    float reductionRatio;
    float maxAcc;               //最大加速度,RPM/s
    float maxDec;               //最大减速度,RPM/s
    float stopDec;              //快停减速度,RPM/s
}MotorAttr_t;           //电机特性 

class MotorDrv
{
public:
    MotorDrv();

    int init(char *dev, MotorAttr_t *attr);
    void exit();
    int useSpeedMode();
    int setAcc(float rpmps);
    int getAcc(float *acc);
    int setDec(float rpmps);
    int getDec(float *Dec);
    int setQuickStopDec(float rpmps);
    int getQuickStopDec(float *Dec);
    int setJerk(float rpmpss);
    int getJerk(float *jerk);
    int setSpeed(float rpm);
    int getSpeed(float *speed);
    int getMaxSpeed(float *maxSpeed);
    int setMaxSpeed(float maxSpeed);
    int setPosition(float pos);
    int getPosition(float *pos);
    int setTorque(float Nm);
    int getTorque(float *Nm);
    CIA402_STATE_e getState(unsigned short param);
    int changeState(CIA402_STATE_e curState, CIA402_STATE_e targetState);

    int test();

private:
    CANConfig can;
    CIA402_STATE_e curState;
    CIA402_STATE_e targetState;
    unsigned int canId;
    MotorAttr_t tMotorAttr;

    int setMotorInfo(unsigned int id, unsigned char *pData, unsigned char len);
    int getMotorInfo(unsigned int id, unsigned char *pData, unsigned char len, int *output);
};


#endif   //_MOTOR_DRV_H__
